#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <unistd.h>
#include <cmath>
#include <cstdlib>
#include "CInitGL.h"
#define ESCAPE 27



bool active=true;
bool fullscreen=true;
bool* keys = new bool[256];

bool light,lp,fp;
float xrot,yrot,xspeed,yspeed,x,y=0.0f,z;
float LightAmbient[]={ 0.5f, 0.5f, 0.5f, 1.0f };
float LightDiffuse[]={ 1.0f, 1.0f,1.0f, 1.0f };
float LightPosition[]={ 0.0f,0.0f,2.0f,1.0f };

CInitGL *CInitGL::instance = 0;

void CInitGL::setInstance(CInitGL *framework) {
	instance = framework;
}


void CInitGL::DisplayWrapper(float dTime)
{
    instance->display(dTime);
}


void CInitGL::InitWrap(int Width,int Height)
{
    instance->Init(Width,Height);
}

/*void CInitGL::FancyRotateWrap(void)
{
    instance->FancyRotate();
}
*/
void CInitGL::DrawGLSceneWrap(void)
{
    instance->DrawGLScene();
}

void CInitGL::ReSizeGLSceneWrap(int Width, int Height)
{
    instance->ReSizeGLScene(Width,Height);
}

void CInitGL::KeyPressedWrap(unsigned char keys, int x, int y)
{
    instance->keyboard(keys,x,y);
}

void CInitGL::SpecialKeyWrap(int key, int x, int y)
{
    instance->specialKeyboard(key,x,y);
}




void CInitGL::Init(int Width,int Height)
{

glShadeModel(GL_SMOOTH);
glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glMatrixMode (GL_MODELVIEW | GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  glMatrixMode(GL_MODELVIEW | GL_PROJECTION);

glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); /////////lights
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);

glEnable(GL_LIGHT1);

    }
void CInitGL::FancyRotate(void)
{

}



void CInitGL::DrawGLScene(void)

    {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef( 0.0f,  0.0f, -10.0f);
glTranslatef( 0.0f, 0.0f, z);
glTranslatef( x, 0.0f, 0.0f );
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON);

  // front
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f(0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f(-1.0f,-1.0f, 1.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(1.0f,-1.0f, 1.0f);

  // right
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f( 1.0f,-1.0f, 1.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f( 1.0f,-1.0f, -1.0f);

  // back
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f( 1.0f,-1.0f, -1.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(-1.0f,-1.0f, -1.0f);

  // left
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(-1.0f,-1.0f,-1.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f(-1.0f,-1.0f, 1.0f);

  glEnd();

  xrot += xspeed;
  yrot += yspeed;

  glutSwapBuffers();


}


void CInitGL::keyboard(unsigned char keys, int x, int y)
{
        usleep(100);
    if (keys == ESCAPE)
    {
      glutDestroyWindow(window);
      }
    if (keys == 'l' && !lp)
        {
            lp = true;
            light=!light;

            if (!light)
                {
                    glDisable(GL_LIGHTING);
                }
        else
        {
           glEnable(GL_LIGHTING);
        }
        }
    else if (keys == 'l' && lp)
        {
            lp = false;
            light = light;

            if (light)
                {
                    glEnable(GL_LIGHTING);   //////////////////////////////double tap 'l' to turn on and off light
                }
                else
                {
                    glDisable(GL_LIGHTING);
                }
        }


    if (!keys == 'l')
        {
            lp=false;
        }
    if (keys == 'f' && !fp)
        {
            fp=true;
            ///////later add texture filter poop stuff here
        }
    if (!keys == 'f')
        {
            fp=false;
        }

    if (keys == 'w')
        {
            z -= 0.03f;
            glutPostRedisplay();
        }
    if (keys == 's' )
        {
            z += 0.03f;
            glutPostRedisplay();
        }
    if (keys == 'd')
        {

            x += 0.03f;
            glutPostRedisplay();
        }
    if (keys == 'a')
        {

            x += 0.03f;
            glutPostRedisplay();
        }

       }


void CInitGL::specialKeyboard(int keys, int x, int y)
{
    if (key == GLUT_KEY_UP)
        {
            xspeed -= 0.001f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_DOWN)
        {
            xspeed += 0.001f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_RIGHT)
        {
            yspeed += 0.001f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_LEFT)
        {
            yspeed -= 0.001f;
            glutPostRedisplay();
        }


    if (key == GLUT_KEY_F1)
        {
            key = false;
        }


}

void CInitGL::ReSizeGLScene(int Width, int Height)
{
    if (Height==0)
    {
        Height=1;
    }

    glViewport(0, 0, Width, Height);
    glMatrixMode(GL_MODELVIEW | GL_PROJECTION);
    glLoadIdentity();

    gluPerspective (45.0f, (GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW );
glutSwapBuffers();
}



void CInitGL::StartInitGL(int argc, char *argv[])
{
    glutInit(&argc, argv);

      glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
      glutInitWindowPosition (0, 0);
    window = glutCreateWindow ("Sector7_0.3 VuVce + Elusive");

    glutDisplayFunc (&DrawGLSceneWrap);
    glutFullScreen();
    glutIdleFunc (&DrawGLSceneWrap);
    glutReshapeFunc (&ReSizeGLSceneWrap);
    glutKeyboardFunc (&KeyPressedWrap);
    glutSpecialFunc (&SpecialKeyWrap);

    Init (640, 480);
    glutMainLoop();


}

